/*
 *  Copyright 2010-2011 Mark Eschbach
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 * $HeadURL$
 * $Id$
 */
package com.meschbach.psi.jetty;

import com.meschbach.psi.PSIException;
import com.meschbach.psi.WebContext;
import java.io.File;
import java.util.LinkedList;
import java.util.List;
import org.mortbay.jetty.webapp.Configuration;
import org.mortbay.jetty.webapp.JettyWebXmlConfiguration;
import org.mortbay.jetty.webapp.TagLibConfiguration;
import org.mortbay.jetty.webapp.WebAppClassLoader;
import org.mortbay.jetty.webapp.WebAppContext;
import org.mortbay.jetty.webapp.WebInfConfiguration;
import org.mortbay.jetty.webapp.WebXmlConfiguration;

/**
 * The <code>JettyWebContext</code> mediates Jetty's internal components to
 * provide the functionality outlined in the PSI's WebContext contract
 * interface.<p>
 * 
 * This class is copyright 2010-2011 by Mark Eschbach and is licensed under the
 * Apache License, Version 2.0; accessible at
 * http://www.apache.org/licenses/LICENSE-2.0.<p>
 * 
 * @author "Mark Eschbach" &lt;meschbach@gmail.com&gt;
 */
public class JettyWebContext implements WebContext {

    File warFile;
    JettyWebContainer container;
    String contextPath;
    WebAppContext context;

    public JettyWebContext(File warFile, JettyWebContainer container, String contextPath) {
        this.warFile = warFile;
        this.container = container;
        this.contextPath = contextPath;
    }

    public String getName() {
        return contextPath;
    }

    public synchronized void start() throws PSIException {
        /*
         * Build the configurations
         */
        List<Configuration> configs = new LinkedList<Configuration>();
        configs.add(new WebXmlConfiguration());
        configs.add(new WebInfConfiguration());
        configs.add(new TagLibConfiguration());
        configs.add(new JettyWebXmlConfiguration());
        /*
         * Create a new Web Application Context
         */
        context = new WebAppContext();
        context.setContextPath(contextPath);
        context.setWar(warFile.getAbsolutePath());
        context.setServer(container.getHttpServer());
        context.setConfigurations(configs.toArray(new Configuration[0]));
        context.setExtractWAR(false);
        context.setCopyWebDir(false);
        /*
         * Setup the class loader
         */
        try {
            WebAppClassLoader wacl = new WebAppClassLoader(getClass().getClassLoader(), context);
            wacl.addClassPath(warFile.toURL().toExternalForm());
            context.setClassLoader(wacl);
        } catch (Throwable t) {
            throw new PSIException(t);
        }
        /*
         * 
         */
        try {
            /*
             * Start the container
             */
            container.addContext(this);
        } catch (Throwable t) {
            throw new PSIException("Error starting Jetty web app", t);
        }
    }

    public void stop() throws PSIException {
        context.setShutdown(true);
        try {
            context.stop();
        } catch (Throwable t) {
            throw new PSIException(t);
        }
    }

    public WebAppContext getJettyContext() {
        return context;
    }

    public void setJettyContext(WebAppContext context) {
        this.context = context;
    }
}
